package com.ld.wms.business.material.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.wms.business.material.mapper.MaterialExampleInventoryMapper;
import com.ld.wms.business.material.model.MaterialExampleInventoryModel;
import com.ld.wms.business.material.service.MaterialExampleInventoryService;
import com.ld.wms.business.material.vo.req.MaterialExampleInventoryReqVo;
import com.ld.wms.business.material.vo.resp.MaterialExampleInventoryRespVo;
import com.ld.wms.business.notice.service.WarnConfigService;
import com.ld.wms.business.notice.vo.resp.WarnConfigRespVo;
import com.ld.wms.business.user.service.UserAccountService;
import com.ld.wms.business.user.vo.resp.UserAccountRespVo;
import com.ld.wms.common.vo.GeneratePageListVo;
import com.ld.wms.sysenum.WarnConfigTypeEnum;
import com.ld.wms.utils.DateUtils;
import com.ld.wms.utils.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 物料库存表，用于存每一条库存信息 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2025-02-24
 */
@Service
public class MaterialExampleInventoryServiceImpl extends ServiceImpl<MaterialExampleInventoryMapper, MaterialExampleInventoryModel> implements MaterialExampleInventoryService {

    @Autowired
    private MaterialExampleInventoryMapper materialExampleInventoryMapper;

    @Autowired
    private JWTUtils jwtUtils;

    @Autowired
    private WarnConfigService warnConfigService;

    @Autowired
    private UserAccountService userAccountService;


    @Override
    public void add(MaterialExampleInventoryReqVo reqVo) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        MaterialExampleInventoryModel model = new MaterialExampleInventoryModel();
        BeanUtils.copyProperties(reqVo, model);
        model.setStatus(7);
        model.setCreateTime(new Date());
        model.setCreateBy(currentUserAccountId);
        materialExampleInventoryMapper.insert(model);
    }

    @Override
    public void updateById(MaterialExampleInventoryReqVo reqVo) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        MaterialExampleInventoryModel model = new MaterialExampleInventoryModel();
        BeanUtils.copyProperties(reqVo, model);
        model.setUpdateTime(new Date());
        model.setUpdateBy(currentUserAccountId);
        materialExampleInventoryMapper.updateById(model);
    }

    @Override
    public MaterialExampleInventoryReqVo getDetails(Long id) {
        MaterialExampleInventoryReqVo reqVo = new MaterialExampleInventoryReqVo();
        MaterialExampleInventoryModel materialExampleInventoryModel = materialExampleInventoryMapper.selectById(id);
        BeanUtils.copyProperties(materialExampleInventoryModel, reqVo);
        // 创建人中文名
        UserAccountRespVo userAccountRespVo = userAccountService.getById(materialExampleInventoryModel.getCreateBy());
        reqVo.setCreateByName(userAccountRespVo.getAccountName());
        return reqVo;
    }

    @Override
    public GeneratePageListVo<MaterialExampleInventoryRespVo> getPageList(Long materialExampleId, Long lockerGridHoleId, Integer current, Integer size) {
        IPage<MaterialExampleInventoryRespVo> iPage = new Page<>(current, size);
        IPage<MaterialExampleInventoryRespVo> materialExampleInventoryRespVoIPage = materialExampleInventoryMapper.selectListByMaterialExampleId(iPage, materialExampleId, null, lockerGridHoleId);
        return new GeneratePageListVo<>(materialExampleInventoryRespVoIPage);
    }

    @Override
    public GeneratePageListVo<MaterialExampleInventoryRespVo> getBeAboutToPageList(Integer current, Integer size) {
        WarnConfigRespVo warnConfigRespVo = warnConfigService.getWarnConfig();
        if (!ObjectUtils.isEmpty(warnConfigRespVo)) {
            Integer configValue = warnConfigRespVo.getMaterialExpireWarnValue();
            Date date = DateUtils.dataAddDay(new Date(), configValue);
            String dataStr = DateUtils.dateToStr(date, null);
            IPage<MaterialExampleInventoryRespVo> iPage = new Page<>(current, size);
            IPage<MaterialExampleInventoryRespVo> materialExampleInventoryRespVoIPage = materialExampleInventoryMapper.selectListByMaterialExampleId(iPage, null, dataStr, null);
            return new GeneratePageListVo<>(materialExampleInventoryRespVoIPage);
        }
        return new GeneratePageListVo<>();
    }


}
